/*
 * PointBrush.java	1.0  01/02/2006
 * PointBrush.java	2.0  10/02/2008
 *
 * This class is the brush drawing point patterns
 * Author:    Isabelle Bichindaritz
 *
 */

import java.awt.*;
import java.nio.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;


public class CircleBrush extends Brush {
	int globalSize;
	public void brushBegin (Point source, Point target, GLAutoDrawable drawable) {
		// initiates brush movement by setting the point size from Painter slider
		int size = Painter.size;
		globalSize = size;
		final GL gl = drawable.getGL();
		final GLU glu = new GLU();
		//System.out.println ("PointBrush brushBegin Size=" + size);
		gl.glPointSize ((float) Painter.fill);
                gl.glDisable(GL.GL_LINE_STIPPLE);
		brushMove (source, target, drawable);	
	}

	public void brushMove (Point source, Point target, GLAutoDrawable drawable) {
		// continues brush movement by copying the color from the original picture 
		// to the current mouse position 
		@SuppressWarnings("unused")
		final GL gl = drawable.getGL();
		@SuppressWarnings("unused")
		final GLU glu =  new GLU();
		if (Painter.imageRGBA == null) {
			System.out.println ("PointBrush picture is NULL");
		}
		else
		{
			setColor (source, drawable);
			circleMidpoint(new Point((int)target.getX(), (int)target.getY()), globalSize, drawable);
		}
	}
	
	void setPixel (double x, double y, GLAutoDrawable drawable)
	   {
		final GL gl = drawable.getGL();
		final GLU glu =  new GLU();
	      gl.glBegin (GL.GL_POINTS);
	         gl.glVertex2d (x, y);
	      gl.glEnd ( );
	   }

	void circleMidpoint (Point center, int radius, GLAutoDrawable drawable)
	   {
	      Point point = new Point();

	      int p = 1 - radius;         // Initial value of midpoint parameter.

	      point.setLocation(0, radius); // Set coordinates for top point of circle.


	      /* Plot the initial point in each circle quadrant. */
	      circlePlotPoints (center, point, drawable);

	      /* Calculate next points and plot in each octant. */
	      while (point.getX() < point.getY()) {
	         point.setLocation(point.getX()+2, point.getY());
	         if (p < 0)
	            p += 2 * point.getX() + 1;
	         else {
	            point.setLocation(point.getX(), point.getY()-2);
	            p += 2 * (point.getX() - point.getY()) + 1;
	         }
	         circlePlotPoints (center, point, drawable);
	      }
	   }

	void circlePlotPoints (Point center, Point point, GLAutoDrawable drawable)
	   {
	      setPixel (center.getX() + point.getX(), center.getY() + point.getY(), drawable);
	      setPixel (center.getX() - point.getX(), center.getY() + point.getY(), drawable);
	      setPixel (center.getX() + point.getX(), center.getY() - point.getY(), drawable);
	      setPixel (center.getX() - point.getX(), center.getY() - point.getY(), drawable);
	      setPixel (center.getX() + point.getY(), center.getY() + point.getX(), drawable);
	      setPixel (center.getX() - point.getY(), center.getY() + point.getX(), drawable);
	      setPixel (center.getX() + point.getY(), center.getY() - point.getX(), drawable);
	      setPixel (center.getX() - point.getY(), center.getY() - point.getX(), drawable);
	   }

	public void brushEnd (Point source, Point target) {
		// ends the brush movement
		int size = Painter.size;

	}


	//----------------------------------------------------
	// This brush draws points
	//----------------------------------------------------

} // end PointBrush class